package com.nondo.rdp.stock.endpoint.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.stock.domain.StockChoose;
import com.nondo.rdp.stock.logic.MarketLogic;
import com.nondo.rdp.stock.model.SearchBean;
import com.nondo.rdp.stock.service.StockChooseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自选股票
 */
@RestController("apiStockCommon")
@RequestMapping("/api/stock/common")
public class CommonEndpoint {
    @Autowired
    MarketLogic marketLogic;

    @Autowired
    StockChooseService stockChooseService;

    @RequestMapping("/search")
    public View search(HttpServletRequest request, HttpServletResponse response, @RequestBody SearchBean searchBean) {
        View view = new View(20000, "获取数据成功");

        User user = UserUtils.checkUser();

        JSONArray searchList = marketLogic.search(searchBean.getKey());


//        List returnList = searchList.stream().filter(item -> {
//            JSONObject itemJson = (JSONObject) JSON.toJSON(item);
//            String code =itemJson.getString("Code") + "";
//            return code.endsWith(".SS") || code.endsWith(".SZ");
//        }).map(item -> {
//            JSONObject itemJson = (JSONObject) JSON.toJSON(item);
//            Map<String, Object> returnItem = new HashMap();
//            returnItem.put("code", itemJson.get("Code"));
//            returnItem.put("name", itemJson.get("ProdName"));
//
//            //判断用户是否加入了自选
//            StockChoose isIn = this.stockChooseService.findByUserAndCode(user, itemJson.get("Code").toString());
//            if (isIn == null) {
//                returnItem.put("choose", 0);
//            } else {
//                returnItem.put("choose", 1);
//            }
//            return returnItem;
//        }).collect(Collectors.toList());

        List returnList = new ArrayList();

        //处理为方便展示的数据
        if (searchList.size() > 0) {
            String code = null;
            Map returnItem = null;
            for (Object item : searchList) {
                JSONObject itemJson = (JSONObject) JSON.toJSON(item);

                //判断是指数还是股票
                if (itemJson.get("securities_type").equals("index")) {
                    continue;
                }

                code = itemJson.get("Code") + "";
                if (code.endsWith(".SS") || code.endsWith(".SZ")) {
                    returnItem = new HashMap();
                    returnItem.put("code", code);
                    returnItem.put("name", itemJson.get("ProdName"));

                    //判断用户是否加入了自选
                    if(user != null && user.getId() != null) {
                        StockChoose isIn = this.stockChooseService.findByUserAndCode(user, itemJson.get("Code").toString());
                        if (isIn == null) {
                            returnItem.put("choose", 0);
                        } else {
                            returnItem.put("choose", 1);
                        }
                    } else {
                        returnItem.put("choose", 0);
                    }


                    returnList.add(returnItem);
                }
            }
        }

        view.setData(returnList);

        return view;
    }
}
